<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h3{
            height: 300px;
        }
    </style>
</head>
<body>
    
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>
    <h3>1</h3>

    <script>
        // //滚动事件
        // window.addEventListener('scroll',function(){//此事件的执行频率过高，纯纯浪费性能
        //     var scrollTop = document.documentElement.scrollTop
        //     console.log(scrollTop)
        // })

        //debounce防抖
        function debounce(fn,delay){
            var Timer = null
            function inner(){
                if(Timer){
                    clearTimeout(Timer)
                }Timer = setTimeout(fn,delay)//利用闭包实现timer的持久化，确保Timer能全局发挥作用
                                             //通过延迟调用和清除已有Timer来确保在一定时间内只有一次输出
            }
            return inner
        }

        function getScrollTop(){
            var scrollTop = document.documentElement.scrollTop

            console.log(scrollTop)
        }

        window.addEventListener('scroll',debounce(getScrollTop,200))
    </script>
</body>
</html>